from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import pytest

@pytest.fixture(scope='session')
def browser():
    # 配置 Chrome 选项
    chrome_options = Options()
    chrome_options.add_argument("--headless")  # 无头模式
    chrome_options.add_argument("--disable-gpu")  # 禁用GPU加速（旧版Chrome可能需要）
    chrome_options.add_argument("--no-sandbox")  # Linux环境下需要
    chrome_options.add_argument("--window-size=1920,1080")  # 设置窗口大小代替maximize_window()
    
    # 配置Service
    service = Service(
        executable_path=r'D:\pulg\webdriver\chromedriver-win64\chromedriver.exe',
        service_args=['--disable-build-check']  # 可选：禁用版本检查
    )
    

    token = 1
    # 初始化浏览器
    if token == 1:
        driver = webdriver.Chrome(service=service)  
        driver.maximize_window() #正常本地启动
    elif token == 2:
        driver = webdriver.Chrome(service=service, options=chrome_options)  #无头模式
    elif token == 3:                                      #远程hub、node启动
        driver = webdriver.Remote(
            command_executor='http://localhost:4444/wd/hub',
            options=chrome_options
        )
        driver.maximize_window()    #无头模式必须关闭全屏
    yield driver
    # 测试结束后清理
    driver.quit()



